<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>任务管理</el-breadcrumb-item>
        <el-breadcrumb-item>签到</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_account_name }}</el-tag>
        </template>

        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            icon="el-icon-delete"
            size="small"
            plain
            @click.stop="Offline(scope.row)"
            >下线</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appAwardContinueSign as baseURL, uploadFile } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "create_time&DESC",
        is_delete: 0 //未删除
      },
      option: {
        delBtn: false,
        editBtn: true,

        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入版本号",
              },
            ],
          },
          {
            label: "奖励类型",
            prop: "award_type",
            type: "select",
            search: false,
            required: true,
            dicData: [
              {
                label: "橘劵",
                value: 1,
              },
              {
                label: "喵币",
                value: 0,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "请选择打赏方式",
              },
            ],
          },
          {
            label: "连续签到3天",
            prop: "day3_reward",
            type: "number",
            span:8,
            required: true,
            rules: [
              {
                required: true,
                message: "请输入3天奖励数量",
              },
            ],
          },
          {
            label: "连续签到5天",
            prop: "day5_reward",
            type: "number",
            span:8,
            required: true,
            rules: [
              {
                required: true,
                message: "请输入5天奖励数量",
              },
            ],
          },
          {
            label: "连续签到7天",
            prop: "day7_reward",
            type: "number",
            span:8,
            required: true,
            rules: [
              {
                required: true,
                message: "请输入7天奖励数量",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot:true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    Offline(row){
      //下线
      this.$confirm("确定下线, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return this.$message.error("操作失败");
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        {...this.params,...this.default},
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done,loading) {
      delete row["id"];
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo
      row.edit_account_name = userInfo.username;
      row.edit_time = new Date();
      let data = await common(baseURL.save, row, null, "POST");
      if (data==null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo
      row.edit_account_name = userInfo.username;
      row.edit_time = new Date();
      let data = await common(baseURL.update, row, null, "POST");
      if (data==null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return this.$message.error("操作失败");
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>